﻿/****************************** Module Header ******************************\
 * Module Name:  TemplateText.cs
 * Module Version:  1.0.0 (2012-7-27)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This componse is use for TemplateText.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace UCL.Web.CustomControls
{
    /// <summary>
    /// 模板文本控件
    /// <para>隐藏模板内容，用于数据调用</para>
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TemplateText runat=server></{0}:TemplateText>")]
    public class TemplateText : HtmlContainerControl
    {
        /// <summary>
        /// 标签,如 div,tr等
        /// </summary>
        public string Tag { get; set; }

        /// <summary>
        /// 是否显示控件内容
        /// </summary>
        public bool Display { get; set; }

        /// <summary>
        /// 注释
        /// </summary>
        public string Comment { get; set; }

        /// <summary>
        /// 预绘制事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (this.Tag != null && this.Tag.ToLower() == "script")
            {
                this.Display = true;
                this.ClientIDMode = ClientIDMode.Static;
            }
            this.Visible = this.Display;
            this.EnableViewState = false;
            base.OnPreRender(e);
        }

        /// <summary>
        /// 重新绘制对象
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (Tag == null) Tag = "div";
            writer.Write("<{0} id=\"{1}\" style=\"display:none;\">".Fmt(Tag, this.ClientID));
            this.RenderChildren(writer);
            writer.Write("</{0}>".Fmt(Tag));
        }

        /// <summary>
        /// 获取当前模板中占位符前面的字符串内容
        /// <para>2012-9-30</para>
        /// </summary>
        /// <returns></returns>
        public string GetInnerHtmlFront()
        {
            StringBuilder sb = new StringBuilder();
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(TemplateText)) break;
                if (c.GetType() == typeof(LiteralControl))
                {
                    sb.Append(((LiteralControl)c).Text);
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取当前模板中占位符后面的字符串内容
        /// <para>2012-10-24</para>
        /// </summary>
        /// <returns></returns>
        public string GetInnerHtmlLast()
        {
            StringBuilder sb = new StringBuilder();
            bool flag = false;
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(TemplateText)) flag = true;
                if (flag && c.GetType() == typeof(LiteralControl))
                {
                    sb.Append(((LiteralControl)c).Text);
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取当前模板中的子控件
        /// <para>2012-9-30</para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetControl<T>(string id = "") where T : Control
        {
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(T))
                {
                    if (id.Length > 0 && id != c.ID) continue;
                    return (T)c;
                }
            }
            return null;
        }

    }
}
